package com.sc.nft.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.*;
import com.sc.nft.entity.*;
import com.sc.nft.entity.vo.CollectionPrivilegeVO;
import com.sc.nft.service.PrivilegeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 服务接口实现
 *
 * @author rdg
 * @since 2022-06-28 16:14:01
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class PrivilegeServiceImpl implements PrivilegeService {
    private final PrivilegeDao privilegeDao;
    private final UserInfoDao userInfoDao;
    private final DigitalCollectionDao digitalCollectionDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void flushUserLevel(Long userId) {
        Integer level = digitalCollectionDao.getUserMaxLevel(userId);
        UserInfo user = userInfoDao.getById(userId);
        if(level != user.getPrivilegeLevel()){
            userInfoDao.setPrivilegeLevel(userId,level);
        }
    }

    @Override
    public List<Privilege> getByCollectionId(Long collectionId) {
        return privilegeDao.getPrivilegeByCollectionId(collectionId);
    }

    @Override
    public Page<CollectionPrivilegeVO> collectionPrivilege(int pageNo, int pageSize, Long collectionId) {
        Page<CollectionPrivilegeVO> collectionPrivilegeVOPage = privilegeDao.collectionPrivilege(pageNo, pageSize, collectionId);
        List<CollectionPrivilegeVO> records = collectionPrivilegeVOPage.getRecords();
        for (CollectionPrivilegeVO record : records) {
            record.setTransferDiscount(record.getTransferDiscount().movePointRight(1).stripTrailingZeros());
        }
        return collectionPrivilegeVOPage;
    }
}